﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gfw.Repository.Admin;
using Gfw.Web.Common;

using Gfw.Models.Admin;

namespace Gfw.Web.Areas.Admin.Controllers
{
    /// <summary>
    /// 用于管理部门
    /// </summary>
    [LAuthorize(RequireRole = "Admin")]
    public class DepartmentController : Controller
    {
        DepartmentRepository _repository = new DepartmentRepository();

      
        public ActionResult Index()
        {
            var list = _repository.GetRoot();
            if (list.Count > 0)
                Params.DepartmentId = list.First().Id;
            else
                Params.DepartmentId = "";
            return View(list);
           
        }

        #region 添加顶级目录
        
        public ActionResult CreateRoot()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateRoot(UsrDepartment organization)
        {
            try
            {
                organization.Id = Guid.NewGuid().ToString();
                organization.Parentid = null;
                _repository.Save(organization);
                return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                return View();
            }
        }
        #endregion

        #region 添加子部门
        //
        // GET: /Department/Create

        public PartialViewResult Create(string id)
        {
            return PartialView();
        }

        //
        // POST: /Department/Create

        [HttpPost]
        public ActionResult Create(string id, UsrDepartment organization)
        {
            try
            {
                // TODO: Add insert logic here
                organization.Parentid = id;
                organization.Id = Guid.NewGuid().ToString();
                _repository.Save(organization);
                return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                return View();
            }
        }
        #endregion

        #region 删除部门
        //
        // GET: /Department/Delete/5

        public ActionResult Delete(string id)
        {

            var model = _repository.Get(id);
            return View(model);
        }

        //
        // POST: /Department/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            try
            {
                _repository.Delete(id);
                return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                return View();
            }
        }
        #endregion

        #region 修改部门
        public ActionResult Edit(string id)
        {

            var model = _repository.Get(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(string id,UsrDepartment organization)
        {
            _repository.Update(organization);
            return RedirectToAction("Index");
        }
        #endregion

    }
}
